package code.structure.module.signIn
{
	import code.main.AppCommonData;
	import code.main.core.mvc.YModelData;
	import code.structure.module.configs.ConfigModel;
	import code.structure.module.configs.data.SignInConfigData;
	import code.structure.module.storage.StorageModel;
	import code.untils.CommandNameConst;
	import code.untils.NetConst;
	import code.untils.NetDataBackKeyConst;
	import code.untils.SystemFunction;
	
	import com.patience.mvc.context.Command;
	import com.patience.net.events.NetEvent;
	
	import flash.utils.Dictionary;

	/**
	 *签到模型 
	 * @author liangqian
	 * 
	 */	
	public class SignInModel extends YModelData
	{
		/**
		 *30天周期循环 
		 */		
		public static const CYCLE_TIME:int = 30;
		private var _configModel:ConfigModel;
		/** 签到静态配置数据 */
		private var _signInConfigDataDic:Dictionary;
		private var _startTime:int;
		private var _signedList:Array;
		private var _totalRewardList:Array;
		private var _storageModel:StorageModel;
		public function SignInModel()
		{
			super();
			_configModel = (_context.commonData as AppCommonData).configModel;
			_storageModel = (_context.commonData as AppCommonData).storageModel;
			_signInConfigDataDic = _configModel.analysisSignIn.dic;
		}
		
		private function initListeners():void{
			_net.addEventListener(NetDataBackKeyConst.SIGN_DATA, analysisSignData);
		}
		
		/**
		 *解析动态数据 
		 * @param obj
		 * 
		 */		
		public function analysisSignData(e:NetEvent):void
		{
			var obj:Object = e.data;
			if(obj.hasOwnProperty('starttime'))
			{
				_startTime = obj.starttime;
				_signedList = obj.signedlist;
				_totalRewardList = obj.totalrewardlist;
			}
			else
			{
				_startTime = SystemFunction.serverTime - 13 * 86400;
				_signedList = [31];
				_totalRewardList = [];
			}
			dispatch(new Command(CommandNameConst.COMMAND_SIGN_DATA_ANALY_COMPLETE));
		}

		/** 初次签到时间 */
		public function get startTime():int
		{
			return _startTime;
		}

		/** 已签天数列表 */
		public function get signedList():Array
		{
			return _signedList;
		}

		/** 已领取奖励列表 */
		public function get totalRewardList():Array
		{
			return _totalRewardList;
		}
		/**
		 *签当天的到 
		 * @param day
		 * 
		 */		
		public function signInHandler(day:int):void
		{
			_netManager.addNetCmdDetail(NetConst.CLASS_USER,NetConst.FUN_SIGN,[day]);
			_signedList.push(day);
			addRewardData(_signedList.length);
		}
		/**
		 *补签 
		 * @param day
		 * 
		 */		
		public function signInOffsetHandler(day:int):void
		{
			_netManager.addNetCmdDetail(NetConst.CLASS_USER,NetConst.FUN_SIGN_OFFSET,[day]);
			_signedList.push(day);
			addRewardData(_signedList.length);
		}
		
		/**
		 *领累积签到的奖励 
		 * @param day 累积签到的天数
		 * 
		 */		
		public function receiveReward(day:int):void
		{
			if(addRewardData(day + CYCLE_TIME))
				_netManager.addNetCmdDetail(NetConst.CLASS_USER,NetConst.FUN_SIGN_GET_TOTAL_REWARD,[day]);
		}
		
		/**
		 * 往仓库增加奖励数据
		 * @param id 签到次数
		 * 
		 */		
		private function addRewardData(id:int):Boolean
		{
			var signInConfigData:SignInConfigData = _signInConfigDataDic[id];
			if(signInConfigData)
			{
				_storageModel.addStorageData(signInConfigData.id,signInConfigData.rewardNum);
				return true;
			}
			return false;
		}
	}
}